package com.cxyzy.camera2demo;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.blankj.utilcode.util.ToastUtils;
import com.cxyzy.camera2library.CameraConfig;
import com.jakewharton.rxbinding2.view.RxView;
import com.tbruyelle.rxpermissions2.RxPermissions;

import static com.cxyzy.camera2demo.Constants.PHOTO_INTERVAL;

public class MainActivity extends AppCompatActivity {
    private final RxPermissions rxPermissions = new RxPermissions(this);

    @SuppressLint("CheckResult")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //配置Camera2相关参数，
        CameraConfig.RECORD_MAX_TIME = 10;
        CameraConfig.RECORD_MIN_TIME = 2;
        CameraConfig.RECORD_PROGRESS_VIEW_COLOR = R.color.colorAccent;
        RxView.clicks(findViewById(R.id.btn_take_photo))
                .compose(rxPermissions.ensure(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE))
                .subscribe(granted -> {
                    if (granted) {
                        Intent intent = new Intent(MainActivity.this, CameraActivity.class);
                        startActivity(intent);
                    } else {
                        ToastUtils.showShort("需要相机和存储权限");
                    }
                });
        RxView.clicks(findViewById(R.id.btn_interval_take_photo))
                .compose(rxPermissions.ensure(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE))
                .subscribe(granted -> {
                    if (granted) {
                        Intent intent = new Intent(MainActivity.this, CameraActivity.class);
                        intent.putExtra(PHOTO_INTERVAL, 10);
                        startActivity(intent);
                    } else {
                        ToastUtils.showShort("需要相机和存储权限");
                    }
                });
    }
}
